package com.zfdang.touchhelper.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.aivin.adovertool.R;
import com.zfdang.touchhelper.bean.AppInformation;
import com.zfdang.touchhelper.config.BaseConfig;
import com.zfdang.touchhelper.config.LyActionTypeConfig;
import com.zfdang.touchhelper.service.TouchHelperService;
import com.zfdang.touchhelper.tool.LyPreferenceTool;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Aivin
 * created on 2023/11/10
 * des:
 */
public class AppSelectDialogTool {
    public void showDialog(Context context, ArrayList<AppInformation> listApp) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        BaseAdapter baseAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return listApp.size();
            }

            @Override
            public Object getItem(int position) {
                return listApp.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.layout_package_information, null);
                    holder = new ViewHolder(convertView);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                AppInformation app = listApp.get(position);
                holder.textView.setText(app.applicationName);
                holder.imageView.setImageDrawable(app.applicationIcon);
                holder.checkBox.setChecked(app.isChecked);
                return convertView;
            }
        };


        View viewAppList = inflater.inflate(R.layout.layout_select_packages, null);
        ListView listView = viewAppList.findViewById(R.id.listView);
        listView.setAdapter(baseAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckBox item = ((ViewHolder) view.getTag()).checkBox;
                AppInformation app = listApp.get(position);
                app.isChecked = !app.isChecked;
                item.setChecked(app.isChecked);
            }
        });


        final AlertDialog dialog = new AlertDialog.Builder(context)
                .setView(viewAppList)
                .create();

        Button btCancel = viewAppList.findViewById(R.id.button_cancel);
        if (btCancel != null) {
            btCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
        }
        Button btConfirm = viewAppList.findViewById(R.id.button_confirm);
        if (btConfirm != null) {
            btConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // save checked packages
                    Set<String> pkgWhitelist = new HashSet<>();
                    for (AppInformation app : listApp) {
                        if (app.isChecked) {
                            pkgWhitelist.add(app.packageName);
                        }
                    }
                    LyPreferenceTool.getInstance().putStringSet(BaseConfig.WHITELIST_PACKAGE, new HashSet<>(pkgWhitelist));

                    // notify accessibility to refresh packages
                    TouchHelperService.dispatchAction(LyActionTypeConfig.ACTION_REFRESH_PACKAGE);
                    dialog.dismiss();
                }
            });
        }

        // show the dialog
        dialog.show();
    }


    static class ViewHolder {
        TextView textView;
        ImageView imageView;
        CheckBox checkBox;

        public ViewHolder(View v) {
            textView = v.findViewById(R.id.name);
            imageView = v.findViewById(R.id.img);
            checkBox = v.findViewById(R.id.check);
        }
    }
}
